﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using QRCoder;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Forms;


namespace PrintDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 导出方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            bool flag = ExportReceipt();
            if (flag)
            {
                MessageBox.Show("生成成功！");
            }
            else
            {
                MessageBox.Show("生成失败，请联系管理员！");
            }
        }
        /// <summary>
        /// 导出生成标签
        /// </summary>
        /// <returns></returns>
        public bool ExportReceipt()
        {
            try
            {

                // 生成二维码的内容
                string strCode = "";
                StringBuilder sb = new StringBuilder();
                sb.Append(txtName.Text);
                sb.Append("$");
                sb.Append(txtName.Text);
                sb.Append("$");
                sb.Append(txtAge.Text);
                sb.Append("$");
                sb.Append(txtAddress.Text);
                sb.Append("$");
                sb.Append(txtNation.Text);
                sb.Append("$$");
                sb.Append(txtQQ.Text);
                sb.Append("$");
                strCode = sb.ToString();
                QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
                QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
                QRCode qrcode = new QRCode(qrCodeData);

                // GetGraphic 第一个参数设置图形的大小
                Bitmap qrCodeImage = qrcode.GetGraphic(3, Color.Black, Color.White, null, 15, 1, false);

                MemoryStream ms = new MemoryStream();
                qrCodeImage.Save(ms, ImageFormat.Jpeg);

                // 保存图片 
                var ImgPath = PdfUtils.GetQRFileName();
                qrCodeImage.Save(ImgPath);

                // 保存pdf文件
                var pdfFilePath = PdfUtils.GetOutputFolderPdf() + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                if (File.Exists(Path.GetFullPath(pdfFilePath)))
                {
                    File.Delete(Path.GetFullPath(pdfFilePath));
                }
                var fileStream = new FileStream(pdfFilePath, FileMode.Create);
                //var pdfDoc = new Document(PageSize.A4);
                var pdfDoc = new Document(new iTextSharp.text.Rectangle(226.4f, 169.8f)); // 80*60 mm
                var pdfWriter = PdfWriter.GetInstance(pdfDoc, fileStream);
                pdfDoc.SetMargins(0.2f, 0.2f, 3.2f, 0.2f);
                pdfDoc.AddAuthor(PdfUtils.Author);
                pdfDoc.Open();

                // 中文字体,解决中文不能显示问题
                BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\SIMSUN.TTC,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                // 五号
                iTextSharp.text.Font fiveFont = new iTextSharp.text.Font(bfChinese, 8f);   // 五号 10.5f 小三号 15     
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ImgPath);
                // 新建表格 3列.
                PdfPTable table0 = new PdfPTable(3)
                {
                    WidthPercentage = 98,// 宽度100%填充,
                };
                table0.DefaultCell.VerticalAlignment = Element.ALIGN_CENTER;

                // 设置列宽
                float[] columnWidths0 = { 0.4f, 0.8f, 0.8f };
                table0.SetWidths(columnWidths0);
                //正文第1行
                PdfUtils.CreateCell(table0, "姓名", fiveFont);                
                PdfUtils.CreateCell(table0, txtName.Text, fiveFont);
                PdfUtils.CreateCell(table0, image, 30, 0, 5);
                PdfUtils.CreateCell(table0, "学历", fiveFont);               
                PdfUtils.CreateCell(table0, txtEducation.Text, fiveFont);         

                
                PdfUtils.CreateCell(table0, "年龄", fiveFont);
                PdfUtils.CreateCell(table0, txtAge.Text, fiveFont);

                PdfUtils.CreateCell(table0, "地址", fiveFont);
                PdfUtils.CreateCell(table0, txtAddress.Text, fiveFont);

                PdfUtils.CreateCell(table0, "民族", fiveFont);
                PdfUtils.CreateCell(table0, txtNation.Text, fiveFont);

                PdfUtils.CreateCell(table0, "QQ号", fiveFont);
                PdfUtils.CreateCell(table0, txtQQ.Text, fiveFont,0,2);
                pdfDoc.Add(table0);
                pdfDoc.Close();
                fileStream.Dispose();

                PdfUtils.VerifyPdfFileIsReadable(pdfFilePath);
                //直接打开pdf文件

                System.Diagnostics.Process.Start(pdfFilePath); 
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtEducation.Text = "南开大学";
            txtName.Text = "小明";
            txtAge.Text = "31";
            txtQQ.Text = "888888";
            txtAddress.Text = "苏州";
            txtNation.Text = "汉族";
        }
    }
}
